<?php

namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use App\crm_customer;
use App\crm_users;
use Encore\Admin\Auth\Database\Permission;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\KehuGender;
class Crm_kehutongjiController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){
            $content->header('客户统计');
            $content->description('统计');
            $user=DB::table("admin_users")->get();
            $maoli=DB::table('crm_customer')->get();
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $maoli[$k]->lirun=0;
                    $deal=DB::table("crm_deal")->where("deal_customer_id",$vo->id)->get();
                    if(!$deal->isEmpty()){
                        foreach($deal as $k=>$vo){
                            $a[$k]=$vo->deal_lirun;
                        }
                        $maoli[$k]->lirun=array_sum($a);
                    }
                }
            }
            for($k=0;$k<=count($maoli);$k++)
            {
                for($j=count($maoli)-1;$j>$k;$j--){
                    if($maoli[$j]->lirun>$maoli[$j-1]->lirun){
                        $temp = $maoli[$j];
                        $maoli[$j] = $maoli[$j-1];
                        $maoli[$j-1] = $temp;
                    }
                }
            }
            $res['laiyuan']=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
            $res['list']=$maoli;
            $res['user']=$user;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $content->body(new KehuGender($res));
        });
    }
   
    public function kehu(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {
            $content->header('客户统计');
            $content->description('统计');
            $user=DB::table("admin_users")->get();
            $maoli=DB::table('crm_customer')->get();
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            $query=DB::table('crm_customer');
            if($request['customer_type']){
                $maoli=$query->where('customer_type',$request['customer_type'])->get();
                $query=$query->where('customer_type',$request['customer_type']);
            }
            if($request['customer_laiyuan']){
                $maoli=$query->where('customer_laiyuan',$request['customer_laiyuan'])->get();
                $query=$query->where('customer_laiyuan',$request['customer_laiyuan']);
            }
            if($request['customer_name']){
                $maoli=$query->where('customer_name','like','%'.$request['customer_name'].'%')->get();
                $query=$query->where('customer_name','like','%'.$request['customer_name'].'%');
            }
            if($request['customer_phone']){
                $maoli=$query->where('customer_phone',$request['customer_phone'])->get();
                $query=$query->where('customer_phone',$request['customer_phone']);
            }
            if($request['customer_danwei']){
                $maoli=$query->where('customer_danwei',$request['customer_danwei'])->get();
                $query=$query->where('customer_danwei',$request['customer_danwei']);
            }
            if($request['bumen']){
                $aa=DB::table('admin_user_permissions')->where("permission_id",$request['bumen'])->pluck("user_id");
                $maoli=$query->whereIn('customer_weihu',$aa)->get();
                $query=$query->whereIn('customer_weihu',$aa);
            }
            if($request['yuangong']){
                $maoli=$query->where('customer_weihu',$request['yuangong'])->get();
                $query=$query->where('customer_weihu',$request['yuangong']);
            }
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $maoli[$k]->lirun=0;
                    $deal=DB::table("crm_deal")->where("deal_customer_id",$vo->id)->get();
                    $dealquery=DB::table("crm_deal")->where("deal_customer_id",$vo->id);
                    if($request['order_lvyoudate']){
                        if($request['order_lvyoudate']['start']){
                            $deal=$dealquery->where("deal_customer_id",$vo->id)->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2))->get();
                            $dealquery=$dealquery->where("deal_customer_id",$vo->id)->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2));
                        }
                        if($request['order_lvyoudate']['end']){
                            $deal=$dealquery->where('deal_dealdate', '<', substr($request['order_lvyoudate']['end'], 2))->get();
                        }
                    }
                    if(!$deal->isEmpty()){
                        foreach($deal as $k=>$vo){
                            $a[$k]=$vo->deal_lirun;
                        }
                        $maoli[$k]->lirun=array_sum($a);
                    }
                }
            }
            for($k=0;$k<=count($maoli);$k++)
            {
                for($j=count($maoli)-1;$j>$k;$j--){
                    if($maoli[$j]->lirun>$maoli[$j-1]->lirun){
                        $temp = $maoli[$j];
                        $maoli[$j] = $maoli[$j-1];
                        $maoli[$j-1] = $temp;
                    }
                }
            }
            $res['laiyuan']=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
            $res['list']=$maoli;
            $res['user']=$user;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $content->body(new KehuGender($res));
        });

    }
    
}
